# Maintainer: Hoang Nguyen <folliekazetani@protonmail.com>
pkgname=zot
pkgver=2.1.0
_zui_commit=c78b303ee88555b39b9f21f136e702724b1725b0
pkgrel=0
pkgdesc="Vendor-neutral OCI-native container image registry"
url="https://zotregistry.dev/"
# 32-bit, s390x, riscv64, loongarch64: tests fail to build
arch="all !armv7 !armhf !x86 !s390x !riscv64 !loongarch64"
license="Apache-2.0"
makedepends="go linux-headers npm nodejs"
subpackages="
	$pkgname-doc
	$pkgname-openrc
	$pkgname-cli:_cli
	$pkgname-exporter:_exporter
	$pkgname-cli-bash-completion:_bashcomp:noarch
	$pkgname-cli-fish-completion:_fishcomp:noarch
	$pkgname-cli-zsh-completion:_zshcomp:noarch
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/project-zot/zot/archive/refs/tags/v$pkgver.tar.gz
	zui-$_zui_commit.tar.gz::https://github.com/project-zot/zui/archive/$_zui_commit.tar.gz
	zot.initd
	zot.confd

	basename_patch
	"
options="net" # download Go modules

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare
	go mod vendor -v
	patch -p1 -i "$srcdir"/basename_patch

	cd "$srcdir"/zui-$_zui_commit
	npm install --no-progress
}

build() {
	cd "$srcdir"/zui-$_zui_commit
	npm run build
	cp -r build "$builddir"/pkg/extensions/

	cd "$builddir"
	local _extensions="sync,search,scrub,metrics,lint,ui,mgmt,profile,userprefs,imagetrust"
	local _goldflags="
	-X zotregistry.io/zot/pkg/api/config.ReleaseTag=v$pkgver
	-X zotregistry.io/zot/pkg/api/config.Commit=AlpineLinux
	-X zotregistry.io/zot/pkg/api/config.BinaryType=${_extensions//,/-}
	-X zotregistry.io/zot/pkg/api/config.GoVersion=$(go env GOVERSION)
	"

	for binary in zot zb zli; do
		go build -v \
			-ldflags "$_goldflags" \
			-tags "$_extensions,containers_image_openpgp" \
			./cmd/$binary
	done
	go build -v -tags containers_image_openpgp ./cmd/zxp

	# Only generate shell completions for the CLI client
	for shell in bash fish zsh; do
		./zli completion $shell > zli.$shell
	done
}

check() {
	# Skip tests requiring static container image tarballs (pulled via skopeo) in ./test/data/
	# and oras binary in ./hack/tools/
	# shellcheck disable=2046
	go test -tags containers_image_openpgp \
		-skip TestHTTPClient \
		$(go list ./... | grep -v \
		-e 'pkg/test/image-utils$' \
		-e 'pkg/test/common$' \
		-e 'pkg/storage/local$')
}

package() {
	install -Dm755 zot -t "$pkgdir"/usr/bin/

	install -Dm644 examples/config-*.json -t "$pkgdir"/usr/share/doc/zot/examples/

	install -Dm755 "$srcdir"/zot.initd -t "$pkgdir"/etc/init.d/zot
	install -Dm644 "$srcdir"/zot.confd -t "$pkgdir"/etc/conf.d/zot
}

_cli() {
	pkgdesc="$pkgdesc - CLI tools"
	install -Dm755 "$builddir"/zli "$builddir"/zb -t "$subpkgdir"/usr/bin/
}

_exporter() {
	pkgdesc="$pkgdesc - metrics exporter"
	install -Dm755 "$builddir"/zxp -t "$subpkgdir"/usr/bin/
}

_bashcomp() {
	pkgdesc="Bash completions for $pkgname-cli"
	install_if="bash-completion $pkgname-cli=$pkgver-r$pkgrel"
	install -Dm644 "$builddir"/zli.bash \
		"$subpkgdir"/usr/share/bash-completion/completions/zli
}

_fishcomp() {
	pkgdesc="Fish completions for $pkgname-cli"
	install_if="fish $pkgname-cli=$pkgver-r$pkgrel"
	install -Dm644 "$builddir"/zli.fish \
		"$subpkgdir"/usr/share/fish/vendor_completions.d/zli.fish
}

_zshcomp() {
	pkgdesc="Zsh completions for $pkgname-cli"
	install_if="zsh $pkgname-cli=$pkgver-r$pkgrel"
	install -Dm644 "$builddir"/zli.zsh \
		"$subpkgdir"/usr/share/zsh/site-functions/_zli
}

sha512sums="
f54484dac486273255308f2a20a70af38f920931361c267d7995474d0edb21fa91a2710b7b23d90f5e47bbbf161abf3737994f9da2e145195afcf1a3fb4a9932  zot-2.1.0.tar.gz
5f71e5162e52ce0b2bb524d74b577fee56e026c880b353b0bd5f0ba82e9eb74ed56dfffd0f8e38a318dcd3740aab8b3485534b2d2d3e77277a4666f6c70d355d  zui-c78b303ee88555b39b9f21f136e702724b1725b0.tar.gz
86dfa273e97110b703fbc368757520ef6169dead185ff54b5b8ba45e764949bc3a21a51d53a1605d7163faef8c83c066e688577f5315b8c0afb09f69a2bbe3bb  zot.initd
b128ecaf3e35cfdab8069da9a8267a9faed50b892ae07b6a4a22a4108236d6cdbc10cc4cf0105c728e9e6da6ac773f56d1dd84de1ba4463110058b6c2f190b30  zot.confd
640526de31a5eb21112c3d3fd30ce78331e5f5998166c9af9405ebad919a9a895ee82e3eed7067c2ce7e3e558e31907398fec1ad895be708e9f8ada696076216  basename_patch
"
